class UsersController < ApplicationController
  # Be sure to include AuthenticationSystem in Application Controller instead
  include AuthenticatedSystem
  
  
  # render new.rhtml
  def new
  end
  
  def create
    @user = User.new(params[:user])
    if @user.valid?
      @user.save
      flash[:cre_user_ss] = "Thanks for register, please login to start the application"
      redirect_to :action=>"new"
    else
      flash[:cre_user_err] = "Registration is not successful created, please try again ..."
      render :action => "new"
    end
  end
  
  def upload
    user = User.find_by_id(session[:user_id])
    user_photo = UserPhoto.new(params[:user])
    user_photo.user = (user)
    if user_photo.save
      redirect_to :controller => "workspaces" , :action => "profile"
    end
  end
  
  def change_picture
    user_photo = UserPhoto.find_by_user_id(session[:user_id])
    user_photo.destroy
    redirect_to :controller => "workspaces" , :action => "profile"
  end
  
  def change_password
    user = User.find_by_id(session[:user_id])
    if User.authenticate(current_user.login, params[:old_password])
      if params[:new_password] == params[:new_password_confirmation] && (!params[:new_password].blank?)
        if user.update_attribute("password", params[:new_password])
          flash[:pass_change] = "Password has been changed"
          redirect_to :controller => "workspaces" , :action => "profile"
        end
      else
        flash[:pass_err] = "New Password mismatch"
        redirect_to :controller => "workspaces" , :action => "profile"
      end
    else
      flash[:pass_err] = "Old password incorrect"
      redirect_to :controller => "workspaces" , :action => "profile"
    end
  end
  
end
